

package com.icloud.admin.application;


import com.icloud.admin.application.command.*;
import com.icloud.admin.domain.*;


import com.icloud.common.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class AccessApplicationService {
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private MenuRepository menuRepository;

    @Autowired
    private ResourceRepository resourceRepository;



    public void createRole(CreateRoleCommand createRoleCommand) {
        Assert.isNull(roleRepository.findByName(createRoleCommand.getName()), "角色名称已存在");
        Role role = new Role(createRoleCommand.getName(), createRoleCommand.getDescription());
        roleRepository.save(role);
    }

    /**
     * 更新角色属性
     *
     * @param changeRolePopsCommand
     */
    public void changeRolePops(ChangeRolePopsCommand changeRolePopsCommand) {
        Role role = roleRepository.findOne(changeRolePopsCommand.getRoleId());
        if (role != null) {
            role.changeProps(changeRolePopsCommand.getName(), changeRolePopsCommand.getDescription());
        }
    }

    /**
     * 创建菜单
     *
     * @param command 创建菜单命令
     */
    public void createMenu(CreateMenuCommand command) {

        Menu parent = null;
        if (command.getParentId() != null) {
            parent = menuRepository.findOne(command.getParentId());
        }

        Menu menu = new Menu(parent,command.getName(),command.getHref(),command.getPosition(),command.getIcon(),command.getCategory(),command.getFrontComponent(),command.getDescription());

        menuRepository.save(menu);
    }

    /**
     * 创建访问资源
     *
     * @param command
     */
    public void createResource(CreateResourceCommand command) {
        Menu menu = menuRepository.findOne(command.getMenuId());
        if(menu!=null){
            Resource resource = new Resource(command.getName(),command.getIdentifer(),command.getCategory(),command.getUri(),command.getMethod(),command.getDescription());
            menu.addResource(resource);
        }
    }



    /**
     * 为角色分配菜单
     *
     * @param assignMenuToRoleCommand 为角色分配菜单命令
     */
    public void assignMenuToRole(AssignMenuToRoleCommand assignMenuToRoleCommand) {
        Role role = roleRepository.findOne(assignMenuToRoleCommand.getRoleId());
        if (role != null) {
            List<String> menuIds = assignMenuToRoleCommand.getMenuIds();
            role.undoAssignedAllMenu();
            for (String menuId : menuIds) {
                Menu menu = menuRepository.findOne(menuId);
                role.assignMenu(menu);
            }
        }
    }


    public void assignResourceToRole(AssignResourceToRoleCommand assignAccessUrlToRoleCommand) {
        Role role = roleRepository.findOne(assignAccessUrlToRoleCommand.getRoleId());
        if (role != null) {
            List<String> urlIds = assignAccessUrlToRoleCommand.getResourceIds();
            role.undoAssignedAllResource();
            for (String urlId : urlIds) {
                Resource resource = resourceRepository.findOne(urlId);
                role.assignResource(resource);
            }
        }
    }


    public void removeRole(String roleId) {
        Role role = roleRepository.findOne(roleId);
        role.disable();
    }

    public void removeMenu(String menuId) {
        Menu menu = menuRepository.findOne(menuId);
        menu.disable();
    }

    public void removeResource(String resourseId) {
        resourceRepository.delete(resourseId);
    }


    public void changeMenuPops(ChangeMenuCommand command) {
        Menu menu = menuRepository.findOne(command.getId());
        if (menu != null) {
            Menu parent = null;
            if (command.getParentId() != null) {
                parent = menuRepository.findOne(command.getParentId());
            }
            menu.changePops(parent,command.getName(),command.getHref(),command.getPosition(),command.getIcon(),command.getCategory(),command.getFrontComponent(),command.getDescription());

        }
    }


    public void changeResource(ChangeResourceCommand command) {
        Resource resource = resourceRepository.findOne(command.getId());
        if (resource != null) {
            resource.changePops(command.getName(),command.getIdentifer(),command.getCategory(),command.getUri(),command.getMethod(),command.getDescription());
        }
    }



}
